package com.nantian.logic.utils;

import com.nantian.model.security.User;
import com.nantian.security.JwtTokenUtil;
import com.nantian.security.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class TokenUtil {
    private String tokenHeader = "Authorization";
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private UserRepository userRepository;

    public String getUserId(HttpServletRequest request) {
        String token = request.getHeader(tokenHeader).substring(7);
        String userId = jwtTokenUtil.getUserIdFromToken(token);
        return userId;
    }

    /**
     * 获取当前用户的所有信息
     */
    public User getUserInfo(HttpServletRequest request){
        String token = request.getHeader(tokenHeader).substring(7);
        String userId = jwtTokenUtil.getUserIdFromToken(token);
        return userRepository.findByUserid(userId);
    }
}
